﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using BusinessLogic;

namespace Interface.Posts
{
    public partial class PostDetails : System.Web.UI.Page
    {
        string stringID;
        DataConnection dc = new DataConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["AdminSession"] != null)
            {
                imbEditPost.Visible = true;
            }  
            getQueryString();
            pageCount(stringID);
        }

        public void getQueryString()
        {
            if (Request.QueryString["ID"] != null)
            {
                stringID = Request.QueryString["ID"].ToString();
                BindPost(stringID);
            }
        }

        protected void BindPost(string ID)
        {
            string sqlStr = "Select * from Post where PostID='"+ID+"'";
            DataTable dt = dc.getData(sqlStr);            
            if (dt.Rows.Count > 0)
            {                                                                
                DataRow row = dt.Rows[0];
                int PostID = Convert.ToInt32(row["PostID"].ToString());
                string author = row["Author"].ToString();
                string sqlAuthorName = "Select Fullname from Users where Username = '"+author+"'";
                string authorName = dc.getStringValue(sqlAuthorName);
                string title = row["Title"].ToString();
                string shortDes = row["ShortDescript"].ToString();
                string fullDes = row["Descript"].ToString();
                string image = row["ImageSource"].ToString();
                string postDate = row["PostDate"].ToString();
                string lastUpdate = row["LastUpdate"].ToString();
                StringBuilder postDetails = new StringBuilder();
                postDetails.AppendFormat(@"<div class='newdetails'>
                                    <h2>{0}</h2>
                                    <span>{1}</span>
                                    <p>{2}</p>
                                    <img src='/Images/Post/{3}' />
                                    <p>{4}</p>
                                    <span>{5}</span>
                                </div>", title, postDate, shortDes, image, fullDes, authorName);

                lit.Text = postDetails.ToString();                
                                        
                
            }
        }

       

        protected void generateHTML()
        {
            StringBuilder builder = new StringBuilder();
        }

        protected void imbEditPost_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("EditPost.aspx?ID=" + stringID);
        }

        protected void pageCount(string postID)
        {
            if (Request.QueryString["ID"] != null)
            {
                string sqlViewed = "Select Viewed from Post where PostID=" + postID;
                int viewed = dc.getNumberValue(sqlViewed);
                viewed = viewed + 1;
                string sqlUpdateViewed = "Update Post set Viewed=" + viewed + " where PostID=" + postID;
                dc.ExecNonQuery(sqlUpdateViewed);
            }
        }
        
    }
}